package com.idp.web.service.impl;

import com.idp.common.base.BusinessException;
import com.idp.common.constants.CommonConstants;
import com.idp.common.constants.Constants;
import com.idp.common.persistence.Page;
import com.idp.web.dao.HospitalsDao;
import com.idp.web.dto.HospitalListDTO;
import com.idp.web.entity.Hospitals;
import com.idp.web.service.IHospitalsService;
import com.idp.web.system.entity.SysUser;
import com.idp.web.system.service.SysUserService;
import com.idp.web.vo.HospitalsSearchVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 医院service实现类
 *
 * @author xubinren
 * @version 1.0
 * @date 2018/2/27
 */
@Service("hospitalsService")
public class HospitalsServiceImpl implements IHospitalsService {

    @Resource
    private HospitalsDao hospitalsDao;

    @Resource
    private SysUserService sysUserService;

    /**
     * 根据查询矫健分页查询对应的医院列表
     * <p>
     * hospitalsSearch 医院查询条件
     *
     * @param page 分页信息
     * @return Page<Hospitals>
     */
    @Override
    public Page<HospitalListDTO> findByPage(HospitalsSearchVo hospitalsSearch, Page<HospitalListDTO> page) {
        page.setResult(hospitalsDao.selectPage(page, hospitalsSearch));
        return page;
    }

    /**
     * 医院查询
     *
     * @param hospitals 医院信息
     * @return List<Hospitals>
     */
    @Override
    public List<Hospitals> findBySearch(Hospitals hospitals) {
        return hospitalsDao.find(hospitals);
    }

    /**
     * 通过ID查询
     *
     * @param id 医院id
     * @return Hospitals
     */
    @Override
    public Hospitals getById(String id) {
        return hospitalsDao.getById(id);
    }

    /**
     * 新增
     *
     * @param hospitals 医院信息
     * @param userId    用户id
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public void add(Hospitals hospitals, String userId) {
        hospitals.preInsert(userId);
        hospitals.setCompleteAddress(
                (StringUtils.isNotEmpty(hospitals.getAdderssProvinceId()) ? hospitals.getAdderssProvinceId() : "") +
                        (StringUtils.isNotEmpty(hospitals.getAdderssCityId()) ? hospitals.getAdderssCityId() : "") +
                        (StringUtils.isNotEmpty(hospitals.getAddressDetail()) ? hospitals.getAddressDetail() : "")
        );
        hospitalsDao.add(hospitals);

        //同步到用户信息
        SysUser user = new SysUser();
        BeanUtils.copyProperties(hospitals,user);
        user.setUsername(hospitals.getHospitalName());
        user.setUserType(CommonConstants.USER_TYPE_HOSPITAL);
        sysUserService.registerAdd(user);
    }

    /**
     * 修改
     *
     * @param hospitals 医院信息
     * @param userId    用户id
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public void update(Hospitals hospitals, String userId) {

        Hospitals oldHospitals = getById(hospitals.getId());

        hospitals.setCompleteAddress(
                (StringUtils.isNotEmpty(hospitals.getAdderssProvinceId()) ? hospitals.getAdderssProvinceId() : "") +
                        (StringUtils.isNotEmpty(hospitals.getAdderssCityId()) ? hospitals.getAdderssCityId() : "") +
                        (StringUtils.isNotEmpty(hospitals.getAddressDetail()) ? hospitals.getAddressDetail() : "")
        );
        hospitals.setCreateUser(oldHospitals.getCreateUser());
        hospitals.setCreateDate(oldHospitals.getCreateDate());
        hospitals.setLogicDelete(oldHospitals.getLogicDelete());
        hospitals.preUpdate(userId);
        hospitalsDao.update(hospitals);

        //同步到用户信息
        SysUser user = new SysUser();
        BeanUtils.copyProperties(hospitals,user);
        user.setUsername(hospitals.getHospitalName());
        user.setUserType(CommonConstants.USER_TYPE_HOSPITAL);
        sysUserService.update(user);
    }

    /**
     * 删除
     *
     * @param id 医院id
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public void delete(String id) {
        hospitalsDao.delete(id);
    }

    /**
     * 根据医院名称查询对应的医院信息：精确查询
     *
     * @param hospitalName 医院名称
     */
    @Override
    public Hospitals getByName(String hospitalName) {
        if (StringUtils.isEmpty(hospitalName)) {
            throw new BusinessException(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }

        //根据医院名称查询对应的医院信息
        return hospitalsDao.getByName(hospitalName);
    }

}